package com.example.demo.controller;

import com.example.demo.common.ResponseMessage;
import com.example.demo.pojo.Customer;
import com.example.demo.service.ICustomerService;
import com.example.demo.until.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;


@RestController
@RequestMapping("/user/")
public class CustomerController {
    @Autowired
    private ICustomerService iCustomerService;
    private Customer customer;

    //1.登入
    @RequestMapping("login.do")
    public ResponseMessage login(HttpSession session, String username, String password) {
        Customer customer = iCustomerService.login(username, password);
        ResponseMessage msg = null;
        if (customer != null) {
            session.setAttribute("currentCustomer", customer);
            msg = new ResponseMessage(0, null, customer);
            return msg;
        } else {
            msg = new ResponseMessage(1, "登入失败");
        }
        return msg;
    }

    //2.注册
    @RequestMapping("register.do")
    public ResponseMessage register(String username, String password, int role, String question, String email, String phone, String answer) {
        Customer customer2 = iCustomerService.select(username);
        ResponseMessage msg = null;
        if (customer2 != null) {
            msg = new ResponseMessage(1, "用户已存在");
        } else {
            customer = new Customer();
            customer.setUsername(username);
            customer.setPassword(password);
            customer.setRole(role);
            customer.setQuestion(question);
            customer.setAnswer(answer);
            customer.setEmail(email);
            customer.setPhone(phone);
            iCustomerService.addCustomer(customer);
            msg = new ResponseMessage(0, "校验成功");
        }
        return msg;
    }

    //    //3.检查登入用户名是否有效
//   @RequestMapping("check_valid.do")
//   public ResponseMessage check_valid(String str){
//        ResponseMessage msg = null;
//       return  null;
//  }
    //4.获取登入用户信息?
    @RequestMapping("get_user_info.do")
    public ResponseMessage User(HttpSession session) {
        Customer user = (Customer) session.getAttribute("currentCustomer");
        ResponseMessage msg = null;
        if (user != null) {
            msg = new ResponseMessage(0, null, user);
        } else
            msg = new ResponseMessage(1, "用户未登录，无法获取当前用户信息");
        return msg;
    }

    //5.忘记密码
    @RequestMapping("forget_get_question.do")
    public ResponseMessage question(String username) {
        Customer customer3 = iCustomerService.select(username);
        String ques = customer3.getQuestion();
        ResponseMessage msg = null;
        if (ques != null) {
            msg = new ResponseMessage(0, null, ques);
        } else
            msg = new ResponseMessage(1, "该用户未设置找回密码问题");
        return msg;
    }

    //6.提交问题答案
    // 7.忘记密码的重设密码
    //8.登入中修改密码?得用用户名进行不然要
    @RequestMapping("reset_password.do")
    public ResponseMessage updatePassword(HttpSession session, String passwordOld, String passwordNew) {
        Customer customer = (Customer) session.getAttribute("currentCustomer");
        String username = customer.getUsername();
        String password = customer.getPassword();
        String md5PasswordOld = MD5Util.getPwd(passwordOld);
        ResponseMessage msg = null;
        if (password.equals(md5PasswordOld)) {
            iCustomerService.updatePassword(username, passwordNew);
            msg = new ResponseMessage(0, "修改密码成功");
        } else {
            msg = new ResponseMessage(1, "旧密码输入错误");
        }
        return msg;
    }

    //9.登入状态更新个人信息
    @RequestMapping("update_information.do")
    public ResponseMessage information(HttpSession session, String email, String phone, String question, String answer) {
        Customer customer = (Customer) session.getAttribute("currentCustomer");
        String username = customer.getUsername();
        ResponseMessage msg = null;
        if (session != null) {
            iCustomerService.updateByInformation(username, email, phone, question, answer);
            msg = new ResponseMessage(0, "更新信息成功");
        } else {
            msg = new ResponseMessage(1, "用户未登入");
        }
        return msg;
    }

    //10.获取当前登录用户的详细信息，并强制登录
    //11.退出登入?
    @RequestMapping("logout.do")
    public ResponseMessage logout(HttpSession session) {
        //session.invalidate();
        //session.invalidate();
        // HttpSession sessions = (HttpSession) session.getAttribute("currentCustomer");
        // sessions.invalidate();
        session.setAttribute("currentCustomer", null);
        Customer Ou = (Customer) session.getAttribute("currentCustomer");
        // System.out.print(Ou);
        ResponseMessage msg = null;
        if (Ou == null) {
            msg = new ResponseMessage(0, "退出成功");
        } else
            msg = new ResponseMessage(1, "服务端异常");
        return msg;
    }
}